package com.ilab.view.widget;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.ilab.R;
import com.ilab.entity.StaticsInfo;
import com.ilab.view.activity.TriEiLabActivity;

public class StatisticPopWin implements OnClickListener {
	private TriEiLabActivity context = null;
	private LayoutInflater layoutInflater = null;
	private View popView = null;
	private TextView tvSensorName = null;
	private TextView text1 = null;
	private TextView text2 = null;
	private int currentIndex = 0;
	private StaticsInfo[] staticsInfos = null;
	public StatisticPopWin(TriEiLabActivity context){
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
		popView = layoutInflater.inflate(R.layout.layout_statistic, null);
		popView.setBackgroundColor(Color.parseColor("#b0000000")); 
		popView.setBackgroundResource(R.drawable.layout_corn_half_bg);
		tvSensorName = (TextView)popView.findViewById(R.id.tv_sensorName);
		text1 = (TextView)popView.findViewById(R.id.tv1);
		text2 = (TextView)popView.findViewById(R.id.tv2);
		((ImageButton)popView.findViewById(R.id.pre)).setOnClickListener(this);
		((ImageButton)popView.findViewById(R.id.next)).setOnClickListener(this);
	}
	
	public void showPopWin(StaticsInfo[] staticsInfos){
		if(staticsInfos==null || staticsInfos.length<1){
			Toast.makeText(context, context.getText(R.string.prompt_nostatics), Toast.LENGTH_SHORT).show();
			return;
		}
		final PopupWindow popup= new PopupWindow(context.findViewById(R.id.trieilab), 500, 200); 
		popup.setContentView(popView);
		popup.setFocusable(true);  
		popup.setOutsideTouchable(true);  
		popup.setBackgroundDrawable(new BitmapDrawable());  
		popup.showAtLocation(context.findViewById(R.id.trieilab_tbar), Gravity.CENTER, 0, 0);
		this.staticsInfos = staticsInfos;
		this.showNext();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.pre:
			showPre();
			break;
		case R.id.next:
			showNext();
			break;
		}
	}
	
	private void showPre(){
		if(staticsInfos==null || staticsInfos.length<1){
			return;
		}
		
		currentIndex--;
		showStaticsInfo(staticsInfos[currentIndex].statics());
		int length = staticsInfos.length;
		if(length==1){
			((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.INVISIBLE);
			((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.INVISIBLE);
		}else{
		
			if(currentIndex<length-1){
				((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.VISIBLE);
			}else{
				((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.INVISIBLE);
			}
			if(currentIndex>0){
				((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.VISIBLE);
			}else{
				((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.INVISIBLE);
			}
		}
	}
	
	private void showNext(){
		if(staticsInfos==null || staticsInfos.length<1){
			return;
		}
		
		showStaticsInfo(staticsInfos[currentIndex].statics());
		int length = staticsInfos.length;
		if(length==1){
			((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.INVISIBLE);
			((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.INVISIBLE);
		}else{
		
			if(currentIndex<length-1){
				((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.VISIBLE);
			}else{
				((ImageButton)popView.findViewById(R.id.next)).setVisibility(View.INVISIBLE);
			}
			if(currentIndex>0){
				((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.VISIBLE);
			}else{
				((ImageButton)popView.findViewById(R.id.pre)).setVisibility(View.INVISIBLE);
			}
		}
		currentIndex++;
	}
	
	public void showStaticsInfo(StaticsInfo s){
		tvSensorName.setText(s.getSensorName()+" ("+s.getSensorUnit()+") ");
		text1.setText(context.getText(R.string.text_maxValue).toString()+s.getMaxValue()+"          "+context.getText(R.string.text_minValue).toString()+s.getMinValue());
		text2.setText(context.getText(R.string.text_difference).toString()+s.getDifference()+"          "+context.getText(R.string.text_avgValue).toString()+s.getAvg());
	}
	
	
}
